package com.sky.controller.user;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/addressBook")
@Api(tags = "地址簿的功能")
@Slf4j
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * @return Result
     * @throws
     * @description 添加地址的功能
     * @author Administrator
     * @param[1] addressBook
     * @time 2023/12/2 17:08
     */
    @ApiOperation("添加地址的功能")
    @PostMapping
    public Result addAddressBook(@RequestBody AddressBook addressBook) {

        log.info("正在执行添加地址的功能.....添加的地址信息为:{}", addressBook);

        addressBookService.addAddressBook(addressBook);

        return Result.success();
    }

    /**
     * @return Result<List < AddressBook>>
     * @throws
     * @description 查询地址的功能
     * @author Administrator
     * @time 2023/12/2 17:08
     */
    @ApiOperation("查询地址的功能")
    @GetMapping("/list")
    public Result<List<AddressBook>> selectAddressBookByUserId() {

        log.info("正在执行查询地址簿的功能........查询的用户id为:{}", BaseContext.getCurrentId());


        List<AddressBook> addressBooks = addressBookService.selectAddressBook();

        return Result.success(addressBooks);

    }

    /**
     * @return Result<AddressBook>
     * @throws
     * @description 查询默认地址的功能
     * @author Administrator
     * @time 2023/12/2 17:08
     */

    @ApiOperation("查询默认地址的功能")
    @GetMapping("/default")
    public Result<AddressBook> selectDefaultAddressBook() {

        log.info("正在执行查询默认地址的功能...........");


        AddressBook addressBook = addressBookService.selectDefaultAddressBook();

        return Result.success(addressBook);
    }

    /**
     * @return Result
     * @throws
     * @description 设置默认地址的功能
     * @author Administrator
     * @param[1] addressBook
     * @time 2023/12/2 17:33
     */

    @ApiOperation("设置默认地址的功能")
    @PutMapping("/default")
    public Result setDefaultAddressBook(@RequestBody AddressBook addressBook) {

        log.info("正在执行设置默认地址的功能..........");

        addressBookService.setDefaultAddressBook(addressBook);

        return Result.success();
    }

    /**
     * @return Result
     * @throws
     * @description 修改地址的功能
     * @author Administrator
     * @param[1] addressBook
     * @time 2023/12/2 17:39
     */

    @ApiOperation("修改地址的功能")
    @PutMapping
    public Result updateAddressBook(@RequestBody AddressBook addressBook) {

        log.info("正在执行修改地址的功能..........");

        addressBookService.updateAddressBook(addressBook);

        return Result.success();
    }

    /**
     * @return Result<AddressBook>
     * @throws
     * @description 根据id查询地址的功能
     * @author Administrator
     * @param[1] id
     * @time 2023/12/2 17:46
     */

    @ApiOperation("根据id查询地址的功能")
    @GetMapping("/{id}")
    public Result<AddressBook> selectAddressById(@PathVariable Long id) {

        log.info("正在执行根据id查询地址的功能.............地址id为:{}", id);
        AddressBook addressBook = addressBookService.selectAddressBookById(id);


        return Result.success(addressBook);

    }

    @ApiOperation("根据id删除地址的功能")
    @DeleteMapping
    public Result deleteAddressBookById(Long id) {

        log.info("正在执行根据id删除地址的功能.......地址id为:{}", id);


        addressBookService.deleteAddressBookById(id);

        return Result.success();
    }

}
